<?php

/***************************************************************\
* FILE:		sys/skins/textonly/main.inc
* SECTION:	skins > textonly
* FUNCTION:	provide main skinning functions
* USES:	
* INCLUDES:	sys/skins/textonly/actionstrings.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

// 

function print_header($menu, $title, $no_menu=0) {
	echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
<html>
	<head>
		<title>$title @ mandragon</title>
		<script language=JavaScript>

function popup(url, w, h) {
	window.open(url, 'mdgpopper', 'width='+w+',height='+h+',title=0,status=0,toolbar=0,directories=0,hotkeys=0,scrolling=1,scrollbars=1,location=0,menubar=0,resizable=1');
}		

		</script>
	</head>
	<body>\n";
	if (!$no_menu)
		print_menu($menu);
	print_title($title);
}

function print_menu($menu) {
	print_submenu($menu, 0, "");
}

function print_submenu($menu, $parent, $urlbase) {

	global $CONFIG;

	if (!is_array($menu[$parent]['children']))
		return;
		
	echo "<ul>\n";
	while ($id = array_shift($menu[$parent]['children'])) {
		$aid = $id;
		while($menu[$aid]['access'] == "INHERIT" and $i++ < $CONFIG['max_directory_depth'])
			$aid = $menu[$id]['parent_id'];
		if ($menu[$aid]['access'] == "DENY")
			break;
		$url = $urlbase."/".$menu[$id]['name'];
		echo "<li><a href=$url>{$menu[$id]['menuname']}</a> [{$menu[$id]['new_items']}/{$menu[$id]['items']}] ". ($menu[$id]['ignored'] ? " <i>ignored</i>" : "") ."\n";
		print_submenu($menu, $id, $url);
	}	
	echo "</ul>\n";
}

function print_itemlist($items, $dirpath) {

	$first = 1;
	
	while ($item = @array_pop($items)) {
		
		if ($first)
			print_sub($item, 1);
		else if ($item['sub_id'] != $prev_sub)
			print_sub($item);

		$prev_sub = $item['sub_id'];

		$first = 0;

		print_item($item, $dirpath);

	}
	echo "</ul>\n";
}

function print_item($item, $dirpath) {

	echo "<li><a href=$dirpath/{$item['item_id']}.{$item['extension']}>{$item['title']}</a> (door {$item['nick_author']})";

}

function print_sub($item, $first = 0) {

	if (!$first)
		echo "</ul>\n";

	echo "<a name=sub{$item['sub_id']}><h3>{$item['sub_title']}</h3></a>\n<ul>";

}

function print_body($body) {
	echo "<p>{$body}";
}

function print_footer($footer, $calctime) {
	if ($footer)
		echo "<p>{$footer}";
die("<p><form method=post action=$PATH_INFO?action=login>
<input name=login><input name=password><input type=submit>
</form>
page done for user {$_SESSION['user_id']} - ".$_SESSION['member']['email']."
<p><a href=/home>/home</a> - <a href=/home?action=logout>logout</a>
<p>this took $calctime s");
}

function print_title($title) {
	echo "<h1>{$title}</h1>\n";
}

function print_form($form) {
	if ((count($form['contents']) < 1) or (count($form['buttons']) < 1))
		return;
	echo "<form name='{$form['name']}' method='{$form['method']}' action='{$form['action']}'";
	if ($form['encoding'])
		echo " enctype='{$form['encoding']}'";
	echo ">\n";
	while ($el = @array_pop($form['contents'])) {
		
		switch($el['type']) {
		
			case "field":
				echo "<p>{$el['title']} <input type=text name={$el['name']} value='".ereg_replace("'", "&#8216;", stripslashes($el['value']))."' size={$el['size']}";
				if ($el['maxlength'])
					echo " maxlength={$el['maxlength']}";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo " />\n";
				break;
				
			case "hidden":
				echo "<input type=hidden name={$el['name']} value='".addslashes($el['value'])."' />\n";
				break;

			case "checkbox":
				echo "<p>{$el['title']} <input type=checkbox name={$el['name']}";
				if ($el['checked'])
					echo " checked='checked'";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onClick=javascript:{$el['onchange']} onKeyDown=javascript:{$el['onchange']}";
				echo " />\n";
				break;

			case "radio":
				echo "<p>{$el['title']}: ";
				while ($k = array_shift($el['keys'])) {
					$v = array_shift($el['values']);
					echo "<input type=radio name={$el['name']} value=$k";
					if ($el['default'] == $k)
						echo " checked='checked'";
					if ($el['readonly'])
						echo " readonly='readonly'";
					if ($el['onchange'])
						echo " onClick=javascript:{$el['onchange']} onChange=javascript:{$el['onchange']}";
					echo " /> $v  \n";
				}
				break;

			case "file":
				echo "<p>{$el['title']} <input type=file name={$el['name']} value='".addslashes($el['value'])."' size={$el['size']}";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo " />\n";
				break;

			case "select":
				echo "<p>{$el['title']} <select name={$el['name']} size={$el['size']}";
				if ($el['readonly'])
					echo " disabled='disabled'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo ">\n";
				if ($el['topvalue'] and !$el['default'])
					echo "<option value=0>-- maak je keuze --";
				while ($key = array_shift($el['options'])) {
					$value = array_shift($el['values']);
					echo "<option value='$key'";
					if ($key == $el['default'])
						echo " selected='selected'";
					echo ">$value</option>";
				}
				echo "</select>\n";
				break;

			case "text":
				echo "<p>{$el['title']}<br /><textarea name={$el['name']} rows={$el['rows']} cols={$el['cols']}";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo ">{$el['value']}</textarea>\n";
				break;
				
			case "time":
				echo "<p>{$el['title']} <input type=text name={$el['name']}_hours value='".ereg_replace("'", "&#8216;", stripslashes($el['default']['hours']))."' size=2 maxlentgth=2";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo " />u<input type=text name={$el['name']}_mins value='".ereg_replace("'", "&#8216;", stripslashes($el['default']['mins']))."' size=2 maxlentgth=2";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo " />\n";
				break;
			
			case "date":
				echo "<p>{$el['title']} <select name={$el['name']}_day size=1";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo ">\n";
				for ($i = 1; $i < 31; $i++) {
					echo "<option value=$i";
					if ($i == $el['default']['day'])
						echo " selected='selected'";
					echo ">$i</option>";
				}
				echo "</select> <select name={$el['name']}_month size=1";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo ">\n";
				for ($i = 1; $i < 12; $i++) {
					echo "<option value=$i";
					if ($i == $el['default']['month'])
						echo " selected='selected'";
					echo ">$i</option>";
				}
				echo "</select> <input type=text name={$el['name']}_year value='";
				if (strlen($el['default']['year']) == 2)
					echo "20{$el['default']['year']}";
				else if ($el['default']['year'])
					echo $el['default']['year'];
				else
					echo "yyyy";
				echo "' maxlength=4 size=4";
				if ($el['readonly'])
					echo " readonly='readonly'";
				if ($el['onchange'])
					echo " onChange=javascript:{$el['onchange']}";
				echo ">\n";
				break;
			
		}
		
	}
	echo "<p>";
	while ($b = @array_pop($form['buttons'])) {
	
		echo "<input type={$b['type']} value='{$b['value']}' ";
		if ($b['size'])
			echo "size={$b['size']} ";
		if ($b['onclick'])
			echo "onClick='{$b['onclick']}' ";
		echo "/>";
	
	}
	echo "\n</form>";
	
}

function print_actions($actions) {

	include_once('sys/skins/textonly/actionstrings.inc');

	global $ACTIONSTR;

	if (is_array($actions)) {
		echo "<p><u>page actions:</u><ul>";
		while ($action = array_pop($actions)) {
			switch($action['type']) {
				case "url":
					echo "<li><a href={$action['action']}>{$ACTIONSTR[$action['id']]}</a>";
					break;
				case "url-action":
					echo "<li><a href={$action['action']}>{$ACTIONSTR[$action['id']]}</a>";
					break;
				case "popup":
					echo "<li><a href=\"javascript:popup('{$action['action']}', 400, 300);\">{$ACTIONSTR[$action['id']]}</a>";
					break;
				case "javascript":
					echo "\n<script lanugage=JavaScript>\n{$action['javascript']}\n</script>\n<li><a href=javascript:{$action['action']}>{$ACTIONSTR[$action['id']]}</a>";
					break;
			}
		}
		echo "</ul>";
	}
}

function print_alerts($alerts) {

	if (is_array($alerts)) {
		echo "<p><u>alerts</u><ul>";
		while ($alert = array_pop($alerts))
			echo "<li><i>{$alert['message']}</i>" . ($alert['link'] ? " <a href={$alert['link']}>link</a>" : "");
		echo "</ul>";
	}
}

function print_article($article) {
	print_body($article['body']);
}

function print_news($news) {
	print_body($news['body']);
}

function print_link($link) {
	print_body($link['body']);
	print_hyperlink($link['link'], $link['link']);
}

function print_download($download) {
	print_body($download['body']);
	print_hyperlink($download['filename'], "downloaden");
	print_body("{$download['downloaded']} keren gedownloaded");
}

function print_hyperlink($url, $string) {
	echo "<p><a href=$url target=_new>$string</a>";
}

function print_poll_results($answers) {
	echo "<ul>";
	while ($a = array_pop($answers))
		echo "<li>{$a['answer']} ({$a['votes']})";
	echo "</ul>";
}
	
function print_subsetlist($subs, $visible, $basedir) {
	echo "<ul>";
	while ($s = array_pop($subs)) {
		$vs = @array_pop($visible);
		if ($s['sub_id'] == $vs['sub_id'])
			echo "<li><a href=#sub{$s['sub_id']}>{$s['sub_title']}</a>";
		else
			echo "<li><a href=$basedir/{$s['sub_id']}.subset>{$s['sub_title']}</a>";
	}
	echo "</ul>";
}

function print_comments($comments) {
	if (is_array($comments) and count($comments) > 0)
		echo "<hr><h4>commentaar:</h4>";
	while ($c = @array_pop($comments))
		echo "<blockquote><b>{$c['posted_by']}</b> ({$c['posted_by_email']})<p>{$c['body']}</blockquote>\n";
}

function print_table($table) {
	echo "<table>\n\t<tr>\n";
	while ($h = array_shift($table['headers'])) {
		$w = current($table['widths']); 
		@next($table['widths']);
		$a = current($table['aligns']);
		@next($table['aligns']);
		echo "\t\t<td";
		if ($w)
			echo " width=$w";
		if ($a)
			echo " align=$a";
		echo "><b>{$h}</b></td>\n";
	}
	echo "\t</tr><tr>\n";
	while ($row = array_shift($table['rows'])) {
		reset($table['widths']);
		reset($table['aligns']);
		while ($td = array_shift($row)) {
			$w = current($table['widths']); 
			@next($table['widths']);
			$a = current($table['aligns']);
			@next($table['aligns']);
			echo "\t\t<td";
			if ($w)
				echo " width=$w";
			if ($a)
				echo " align=$a";
			echo ">$td</td>\n";
		}
		echo "\t</tr><tr>\n";
	}
	echo "\t</tr>\n</table>";
}

function print_event($event) {
	print_body($event['description']);
	echo "<p><b>{$event['activity_type']}</b> in <i>{$event['location_name']}</i><br>van {$event['date_start']} tot {$event['date_stop']}";
}		

?>